package com.xiaoshuidi.cloud.module.report.service.datamanage;

import com.xiaoshuidi.cloud.module.report.controller.datamanage.enums.OverdueScopeEnum;
import com.xiaoshuidi.cloud.module.report.controller.datamanage.vo.*;
import com.xiaoshuidi.cloud.module.report.dal.mapper.datamanage.FinancialAnalysisMapper;
import com.xiaoshuidi.cloud.module.report.utils.ApartmentUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.xiaoshuidi.cloud.module.report.service.datamanage.LeaseAnalysisServiceImpl.getYearMonthList;

/**
 * 财务分析
 */
@Slf4j
@Service
public class FinancialAnalysisServiceImpl implements FinancialAnalysisService {


    @Resource
    private FinancialAnalysisMapper financialAnalysisMapper;

    @Resource
    private ApartmentUtils apartmentUtils;


    /**
     * 应赋逾期
     *
     * @param vo
     * @return
     */
    @Override
    public OverdueColumnChartRespVO accountsReceivableOverdue(LeaseAnalysisReqVO vo) {
        vo.setApartmentIds(apartmentUtils.getAllIdsWhenIdsIsNull(vo.getApartmentIds()));
        List<OverdueScopeRespVO> overdueScopeRespVOS = financialAnalysisMapper.accountsReceivableOverdue(vo);

        List<ColumnChartRespVO> chartRespVOS =  Arrays.stream(OverdueScopeEnum.values()).map(m->{
            ColumnChartRespVO respVO = new ColumnChartRespVO();
            respVO.setName(m.getValue());
            overdueScopeRespVOS.stream().filter(f->f.getOverdueScope().equals(m.getValue())).findFirst().ifPresent(
                    p->{
                        respVO.setValue(p.getReceivableAmount());
                    }
            );
            return respVO;
        }).collect(Collectors.toList());
        OverdueColumnChartRespVO respVO = new OverdueColumnChartRespVO();
        respVO.setColumnChartRespVOS(chartRespVOS);
        respVO.setTotalAmount(chartRespVOS.stream().map(ColumnChartRespVO::getValue)
                .reduce(BigDecimal.ZERO, BigDecimal::add));
         return respVO;
    }

    /**
     * 逾期付款
     *
     * @param vo
     * @return
     */
    @Override
    public OverdueColumnChartRespVO overduePayments(LeaseAnalysisReqVO vo) {
        vo.setApartmentIds(apartmentUtils.getAllIdsWhenIdsIsNull(vo.getApartmentIds()));
        List<OverdueScopeRespVO> overdueScopeRespVOS = financialAnalysisMapper.overduePayments(vo);

        List<ColumnChartRespVO> chartRespVOS =  Arrays.stream(OverdueScopeEnum.values()).map(m->{
            ColumnChartRespVO respVO = new ColumnChartRespVO();
            respVO.setName(m.getValue());
            overdueScopeRespVOS.stream().filter(f->f.getOverdueScope().equals(m.getValue())).findFirst().ifPresent(
                    p->{
                        //取反
                        respVO.setValue(p.getReceivableAmount().negate());
                    }
            );
            return respVO;
        }).collect(Collectors.toList());
        OverdueColumnChartRespVO respVO = new OverdueColumnChartRespVO();
        respVO.setColumnChartRespVOS(chartRespVOS);
        respVO.setTotalAmount(chartRespVOS.stream().map(ColumnChartRespVO::getValue)
                .reduce(BigDecimal.ZERO, BigDecimal::add));
        return respVO;
    }

    /**
     * 租金营收分析
     *
     * @param vo
     * @return
     */
    @Override
    public RentRevenueAnalysisRespVO rentRevenueAnalysis(LeaseAnalysisReqVO vo) {
        vo.setApartmentIds(apartmentUtils.getAllIdsWhenIdsIsNull(vo.getApartmentIds()));
        RentRevenueAnalysisRespVO  respVO =  new RentRevenueAnalysisRespVO();
        LocalDate now = LocalDate.now();
        vo.setYearTimeStr(String.valueOf(now.getYear()));
        //年度租金
        BigDecimal yearRentAmount = financialAnalysisMapper.rentalSum(vo);
        respVO.setYearRentAmount(yearRentAmount);
        //月度租金
        vo.setYearTimeStr(null);
        vo.setMonthlyTimeStr(now.toString().substring(0, 7));
        BigDecimal monthlyRentAmount = financialAnalysisMapper.rentalSum(vo);
        respVO.setMonthlyRentAmount(monthlyRentAmount);

        List<String> yearMonthList = getYearMonthList(vo);
        //树状图
        List<RentalIncomeVO> rentalIncomeVOS = financialAnalysisMapper.rentalIncome(vo);
        respVO.setColumnChartRespVOS(yearMonthList.stream().map(m->{
            ColumnChartRespVO chartRespVO = new ColumnChartRespVO();
            chartRespVO.setName(m);
            rentalIncomeVOS.stream().filter(f->f.getAnalysisTime().equals(m)).findFirst().ifPresent(p->{
                chartRespVO.setValue(p.getAmount());
            });
            return chartRespVO;
        }).collect(Collectors.toList()));
        return respVO;
    }

    /**
     * 收缴率分析
     *
     * @param vo
     * @return
     */
    @Override
    public ContractRentOrderRateRespVO rentOrderRateAnalysis(LeaseAnalysisReqVO vo) {
        vo.setApartmentIds(apartmentUtils.getAllIdsWhenIdsIsNull(vo.getApartmentIds()));
        ContractRentOrderRateRespVO  respVO =  new ContractRentOrderRateRespVO();
        LocalDate now = LocalDate.now();
        vo.setYearTimeStr(String.valueOf(now.getYear()));
        //年度租金
        BigDecimal yearRentAmount = financialAnalysisMapper.rentOrderRateSum(vo);
        respVO.setYearRate(yearRentAmount);
        //月度租金
        vo.setYearTimeStr(null);
        vo.setMonthlyTimeStr(now.toString().substring(0, 7));
        BigDecimal monthlyRentAmount = financialAnalysisMapper.rentOrderRateSum(vo);
        respVO.setMonthlyRate(monthlyRentAmount);

        List<String> yearMonthList = getYearMonthList(vo);
        //树状图
        List<CollectionRateVO> rentalIncomeVOS = financialAnalysisMapper.rentOrderRate(vo);
        respVO.setColumnChartRespVOS(yearMonthList.stream().map(m->{
            ColumnChartRespVO chartRespVO = new ColumnChartRespVO();
            chartRespVO.setName(m);
            rentalIncomeVOS.stream().filter(f->f.getAnalysisTime().equals(m)).findFirst().ifPresent(p->{
                chartRespVO.setValue(p.getRate());
            });
            return chartRespVO;
        }).collect(Collectors.toList()));
        return respVO;
    }

    /**
     * 累计已收统计
     *
     * @param vo
     * @return
     */
    @Override
    public AccumulatedReceivedVO accumulatedReceivedSum(LeaseAnalysisReqVO vo) {
        vo.setApartmentIds(apartmentUtils.getAllIdsWhenIdsIsNull(vo.getApartmentIds()));
        getYearMonthList(vo);
        return financialAnalysisMapper.accumulatedReceivedSum(vo);
    }
}
